package com.kz.himalaya.fragment.homepage;


import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.test.mock.MockApplication;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.kz.himalaya.BR;
import com.kz.himalaya.R;
import com.kz.himalaya.adapter.CommentAdapter;
import com.kz.himalaya.application.MyApplication;
import com.kz.himalaya.bean.details.DetailData;
import com.kz.himalaya.bean.details.DetailEntity;
import com.kz.himalaya.bean.details.Recs;
import com.kz.himalaya.databinding.FragmentDetailBinding;
import com.kz.himalaya.interfaces.HttpService;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class DetailFragment extends Fragment implements Callback<DetailEntity> {


    private CommentAdapter<Object> adapter;
    private long albumId;

    public DetailFragment() {
        // Required empty public constructor
    }

    public static DetailFragment newInstance(long albumId) {

        Bundle args = new Bundle();
        args.putLong("albumId",albumId);
        DetailFragment fragment = new DetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        albumId = bundle.getLong("albumId",-1);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        FragmentDetailBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_detail,container,false);
        HashMap<Class<?>, Pair<Integer, Integer>> map = new HashMap<>();
        map.put(DetailData.Detail.class,Pair.create(R.layout.detail_content_intro, BR.contentIntro));
        map.put(DetailData.User.class,Pair.create(R.layout.detail_anchor_introduce,BR.anchorIntroduce));
        map.put(Recs.class,Pair.create(R.layout.detail_recommend,BR.detailRecommend));
        adapter = new CommentAdapter<>(map,new ArrayList<>(),getContext());
        binding.listDetail.setAdapter(adapter);
        HttpService service = ((MyApplication) getActivity().getApplication()).getService();
        if (albumId!=-1) {
            service.getDetailData(albumId).enqueue(this);
        }
        return binding.getRoot();
    }

    @Override
    public void onResponse(Call<DetailEntity> call, Response<DetailEntity> response) {
        DetailEntity body = response.body();
        if (body != null) {
            DetailData data = body.getData();
            adapter.add(data.getDetail());
            adapter.add(data.getUser());
            adapter.add(data.getRecs());
        }
    }

    @Override
    public void onFailure(Call<DetailEntity> call, Throwable t) {
        t.printStackTrace();
        Log.i("1620", "onFailure: "+t.getMessage());
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
